package com.mybatis.util;


import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


/*
数据转换工具类
用于将数据库查询出的数据转化为页面展示所需的数据
* */
public class DataUtil {

    public static Object[][] getConvertData(List list) {
        Object[][] objects = new Object[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            objects[i] = getArr(list.get(i));
        }
        return objects;
    }

    public static Object[] getArr(Object t) {
        if (t == null) return null;
        Class<?> cls = t.getClass();
        List list = new ArrayList();
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            try {
                PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), cls);
                Method method = descriptor.getReadMethod();
                list.add(method.invoke(t));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Object[] objects = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            objects[i] = list.get(i);
        }
        return objects;
    }

}
